﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

// TODO: Complete documentation
namespace ComicTracker.Models
{
    [Table("publishers")]
    public class Publisher : IComparable<Publisher>
    {
        private Guid _id;
        private string _name;
        private string _address_line1;
        private string _address_line2;
        private string _city;
        private string _state;
        private string _zip_code;
        private string _country;
        private string _sort_name;
        private List<Series> _series;

        #region Properties

        /// <summary>
        /// Gets or sets the unique identifier for this instance.
        /// </summary>
        [Column("id")]
        public Guid ID
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// Gets or sets the name of the publisher represented by this 
        /// instance.
        /// </summary>
        [Column("name")]
        [Required(ErrorMessage="A name is required.")]
        [Display(Name="Name")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Gets or sets the first line of the address for the publisher 
        /// represented by this instance.
        /// </summary>
        [Column("address_line1")]
        [Display(Name="Address Line 1")]
        public string AddressLine1
        {
            get { return _address_line1; }
            set { _address_line1 = value; }
        }

        /// <summary>
        /// Gets or sets the second line of the address for the publisher
        /// represented by this instance.
        /// </summary>
        [Column("address_line2")]
        [Display(Name="Address Line 2")]
        public string AddressLine2
        {
            get { return _address_line2; }
            set { _address_line2 = value; }
        }

        /// <summary>
        /// Gets or sets the city of the address for the publisher represented
        /// by this instance.
        /// </summary>
        [Column("city")]        
        public string City
        {
            get { return _city; }
            set { _city = value; }
        }

        /// <summary>
        /// Gets or sets the state of the address for the publisher represented
        /// by this instance.
        /// </summary>
        [Column("state")]
        public string State
        {
            get { return _state; }
            set { _state = value; }
        }

        /// <summary>
        /// Gets or sets the zip code of the address for the publisher 
        /// represented by this instance.
        /// </summary>
        [Column("zip_code")]
        [Display(Name="Zip Code")]
        public string ZipCode
        {
            get { return _zip_code; }
            set { _zip_code = value; }
        }

        /// <summary>
        /// Gets or sets the country of the address for the publisher 
        /// represented by this instance.
        /// </summary>
        [Column("country")]
        public string Country
        {
            get { return _country; }
            set { _country = value; }
        }

        /// <summary>
        /// Gets or sets the name used for sorting for this instance.
        /// </summary>
        [Column("sort_name")]
        [Display(Name = "Sort Name")]
        public string SortName
        {
            get { return _sort_name; }
            set { _sort_name = value; }
        }

        /// <summary>
        /// Gets or sets a collection of the Series published by the publisher
        /// represented by this instance.
        /// </summary>
        public virtual List<Series> Series
        {
            get 
            {
                if (_series != null)
                    _series.Sort();
                return _series; 
            }
            set { _series = value; }
        }
        #endregion

        /// <summary>
        /// Compares the current instance with another object of the same type 
        /// and returns an integer that indicates whether the current instance 
        /// precedes, follows, or occurs in the same position in the sort order
        /// as the other object.
        /// </summary>
        /// <param name="other">An object to compare with this instance.</param>
        /// <returns>
        /// A value indicating the relative order of the objects being compared.
        /// </returns>
        public int CompareTo(Publisher other)
        {
            if (other == null)
                return 1;

            if (!String.IsNullOrWhiteSpace(this.SortName))
            {
                if (!String.IsNullOrWhiteSpace(other.SortName))
                    return this.SortName.CompareTo(other.SortName);
                else
                    return this.SortName.CompareTo(other.Name);
            }
            else
            {
                if (!String.IsNullOrWhiteSpace(other.SortName))
                    return this.Name.CompareTo(other.SortName);
                else
                    return this.Name.CompareTo(other.Name);
            }
        }
    }
}